var pollingInterval = '3s';
var thresholds = {clarity: .5, pacing: {low: .25, high: .75}};
var maySendAlert = {clarity: true, pacing: true};

function togglePolling(event,direction)
{
	if(direction.direction === 'in')
	{
		// start polling
		$(document).everyTime(pollingInterval,'metricsPoller',updateMetrics);
		$(document).everyTime(pollingInterval,'messagesPoller',updateMessages);
	}
	else
	{
		// stop polling
		$(document).stopTime('metricsPoller');
		$(document).stopTime('messagesPoller');
	}
}

function toggleMessagesPollingForDetailView(event,direction)
{
	if(direction.direction === 'in')
	{
		// start polling
		$(document).everyTime(pollingInterval,'detailViewPoller',updateMessages);
	}
	else
	{
		// stop polling
		$(document).stopTime('detailViewPoller');
	}
}

// this do-nothing function is re-defined in the professorApp.js file
function alertProfessor(){ console.log('alert!'); }

function updateMetrics(i)
{
	$.getJSON('php/metrics.php',{action:'get_metrics', uniqueCourseNumber:currentClass.uniqueCourseNumber},
		function(result)
		{
			// update the class sliders to the correct css position
			setClassSliders(result);
			
			// if professor, check whether to display an alert
			if(isProfessor)
			{
				// if the clarity is too low
				if (result.clarity < thresholds.clarity)
				{
					if(maySendAlert.clarity)
					{
						alertProfessor('The clarity rating is ' + Math.round(result.clarity * 100) + '%. 100% is perfectly clear, 0% is completely unclear.');
						maySendAlert.clarity = false;
					}
				}
				else
				{
					maySendAlert.clarity = true;
				}
				
				// if the pacing is too fast or slow
				if (result.pacing > thresholds.pacing.high || result.pacing < thresholds.pacing.low)
				{
					if(maySendAlert.pacing)
					{
						alertProfessor('The pacing rating is ' + Math.round(result.clarity * 100) + '%. 100% too fast, 0% is too slow, 50% is just right.');
						maySendAlert.pacing = false;
					}
				}
				else
				{
					maySendAlert.pacing = true;
				}
			}
		}
	);
}